<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../../../lib/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
  <div class="layuimini-main">

    <!-- 搜索栏 -->
    <form class="layui-form" lay-filter="searchForm" style="margin-bottom: 20px;">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">状态</label>
          <div class="layui-input-inline">
            <select name="replyFlag" lay-verify="" id="replyFlag">
              <option value="">请选择状态</option>
              <option value="true">已回复</option>
              <option value="false">未回复</option>
            </select>
          </div>
        </div>
        <div class="layui-inline" style="float: right;">
          <button class="layui-btn layui-bg-blue" lay-submit lay-filter="data-search-btn">查询</button>
          <button type="reset" class="layui-btn layui-btn-primary" id="resetBtn">重置</button>
        </div>
      </div>
    </form>
    <!-- 表格 -->
    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    <script type="text/html" id="currentTableBar">
      <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="check">查看</a>
      {{#  if (!d.replyFlag) { }}
      <a class="layui-btn layui-btn-warm layui-btn-normal layui-btn-xs" lay-event="reply" style="width: 60px" >回复</a>
      {{#  } else { }}
      <a class="layui-btn layui-btn-warm layui-btn-normal layui-btn-xs"lay-event="editReply"style="width: 60px" >编辑回复</a>
      {{#  } }}
      <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete" >删除</a>
    </script>
  </div>
</div>

<script src="../../../lib/layui/layui.js" charset="utf-8"></script>
<script src="../../../api/common.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var pageParams = {
            // pageNum: 1,
            // pageSize: 10,
            replyFlag: ''
        };

        // 渲染表格
        function loadTable(params) {
            console.log(params)
            table.render({
                elem: '#currentTableId',
                url: local + 'mailbox/page/list',
                method: "post",
                contentType: 'application/json;charset=UTF-8',
                headers: {'Authorization': `Bearer ${ localStorage.getItem("token") }`},
                where: params,
                request: {
                    pageName: 'pageNum',
                    limitName: 'pageSize'
                },
                cols: [[
                    {type: 'numbers', title: '序号', width: 60, fixed: 'left'},
                    {field: 'name', title: '名称'},
                    {field: 'tel', title: '电话'},
                    {field: 'mail', title: '邮箱'},
                    {field: 'createDate', title: '时间'},
                    {field: 'replyFlag', title: '状态', width: 120, templet: function (d) {
                            return d.replyFlag ? '已回复' : '未回复';
                        }},
                    {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center", fixed: 'right'}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 10,
                page: true,
                unresize: false,
                skin: 'line',
                parseData: function (res) {
                    return {
                        "code": res.code === 200 ? 0 : res.code,
                        "msg": res.message,
                        "count": res.data.total,
                        "data": res.data.records
                    };
                },
            });
        }

        // 初次加载表格数据
        loadTable(pageParams);
        // 搜索按钮监听
        form.on('submit(data-search-btn)', function (data) {
            var replyFlag = data.field.replyFlag;
            pageParams.replyFlag = replyFlag ? replyFlag === 'true' : '';
            pageParams.pageNum = 1; // 重置为第一页
            loadTable(pageParams); // 重新加载表格数据

            return false;
        });

        // 重置按钮监听
        $('#resetBtn').on('click', function () {
            form.val('searchForm', { replyFlag: '' }); // 清空表单
            pageParams.replyFlag = '';
            pageParams.pageNum = 1;
            loadTable(pageParams); // 重新加载表格数据
            console.log(pageParams)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'check') {
                var index = layer.open({
                    title: '查看',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    data: {id: '1234'},
                    area: ['100%', '100%'],
                    content: './Detail.html?id='+ data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                $.ajax({
                    type: 'POST',
                    url: local + 'mailbox/delete',
                    data: JSON.stringify({id:data.id}),
                    dataType: "json",
                    headers: {
                        'Authorization': `Bearer ${ localStorage.getItem("token") }`,
                    },
                    contentType: 'application/json;charset=UTF-8',
                    success: function (res) {
                        if (res.code === 200) {
                            layui.table.reload('currentTableId')
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, {icon: 5});
                    }
                });
            }else if(obj.event === 'reply'){
                var index = layer.open({
                    title: '查看详情',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    data: {id: '1234'},
                    area: ['50%', '50%'],
                    content: './reply.html?id='+ data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }else if(obj.event === 'editReply'){
                var index = layer.open({
                    title: '查看详情',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    data: {id: '1234'},
                    area: ['50%', '50%'],
                    content: './editReply.html?id='+ data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;

            }
        });

    });
</script>

</body>
</html>
